package com.fan.configserver.service;

import com.fan.configserver.mapper.ResourceMapper;
import com.fan.configserver.mapper.ResourceUserMapper;
import com.fan.configserver.mapper.AppResourceRelevanceMapper;
import com.fan.configserver.po.AppResourceRelevance;
import com.fan.configserver.po.AppResourceRelevanceExample;
import com.fan.configserver.po.Application;
import com.fan.configserver.po.Resource;
import com.fan.configserver.util.ConvertUtil;
import com.fan.configserver.vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/9/4 14:53
 * @UpdateUser: hf
 * @UpdateDate: 2018/9/4 14:53
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Service
@Slf4j
public class ResourceService {
    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    ResourceUserMapper resourceUserMapper;
    @Autowired
    AppResourceRelevanceMapper appResourceRelevanceMapper;
    @Autowired
    ResourceEnvService resourceEnvService;


    public JsonPage<ResourceVo> getAllApplicationByPage(MyPage myPage, Long userId, Integer type) {
        Page<ResourceVo> page = PageHelper.startPage(myPage.getPage(), myPage.getResults(), true);
        List<Application> permList = resourceUserMapper.selectResourceByUserId(userId, type);
        List<ResourceVo> result = new ArrayList<ResourceVo>();
        JsonPage<ResourceVo> resultList = new JsonPage<ResourceVo>(page);
        for (Application application : permList) {
            ResourceVo ResourceVo = new ResourceVo();
            BeanUtils.copyProperties(application, ResourceVo);
            result.add(ResourceVo);
        }
        resultList.setRows(result);
        return resultList;
    }

    public List<ResourceVo> getAllResource() {
        List<Resource> selectAll = resourceMapper.selectByExample(null);
        List<ResourceVo> copyTo = ConvertUtil.copyTo(selectAll, ResourceVo.class);
        return copyTo;
    }


    public void addResource(ResourceRequest resourceRequest) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceRequest, resource);
        resourceMapper.insertSelective(resource);
    }

    public void delResource(Long resourceId) {
        resourceMapper.deleteByPrimaryKey(resourceId);
    }

    public void updateResource(ResourceRequest resourceRequest) {
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceRequest, resource);
        resourceMapper.updateByPrimaryKeySelective(resource);
    }


    /**
     * 根据applicationid和applicationEnvId获取当前应用环境的依赖资源
     *
     * @param applicationId
     * @param applicationEnvId
     * @return
     */
    public Map<Long, Long> getRelativeResourceSure(Long applicationId, Long applicationEnvId) {
        Map<Long, Long> mapResult = new HashMap<Long, Long>();
        AppResourceRelevanceExample appResourceRelevanceExample = new AppResourceRelevanceExample();
        AppResourceRelevanceExample.Criteria criteria = appResourceRelevanceExample.createCriteria();
        criteria.andApplicationEnvIdEqualTo(applicationEnvId);
        criteria.andApplicationIdEqualTo(applicationId);
        List<AppResourceRelevance> appResourceRelevances = appResourceRelevanceMapper.selectByExample(appResourceRelevanceExample);
        for (AppResourceRelevance appResourceRelevance : appResourceRelevances) {
            mapResult.put(appResourceRelevance.getResourceId(), appResourceRelevance.getApplicationEnvId());
        }
        return mapResult;
    }


    /**
     * 获取当前所有资源及应用环境的关联资源
     *
     * @param applicationId
     * @param applicationEnvId
     * @return
     */

    public JsonPage<ResourceVo> getAllResourceAndEnv(Long applicationId, Long applicationEnvId) {
        JsonPage<ResourceVo> result = new JsonPage<ResourceVo>();
        Map<Long, Long> relation = this.getRelativeResourceSure(applicationId, applicationEnvId);
        List<ResourceVo> allResource = this.getAllResource();
        for (ResourceVo resourceVo : allResource) {
            List<ResourceEnvVo> envs = resourceEnvService.getAllResourceEnvById(resourceVo.getResourceId());
            Long resEnvId = relation.get(resourceVo.getResourceId());
            if (resEnvId != null) {
                for (ResourceEnvVo resourceEnvVo : envs) {
                    if (resourceEnvVo.getResourceId().longValue() == resEnvId.longValue()) {
                        resourceEnvVo.setSelected(true);
                    }
                }
            }
            resourceVo.setResourceEnvs(envs);
        }
        result.setRows(allResource);
        result.setPage(1);
        result.setTotal(new Long(allResource.size()));
        return result;
    }
}
